package com.ogawa.a7517.fragment;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.ogawa.a7517.Constant;
import com.ogawa.a7517.R;
import com.ogawa.a7517.adapter.FragmentAdapter;
import com.ogawa.a7517.bean.DeviceStateBean;
import com.ogawa.a7517.bean.MassageArmchair;
import com.ogawa.a7517.utils.ViewUtil;
import com.ogawa.a7517.widget.ArcSeekBar;
import com.ogawa.a7517.widget.GasView;
import com.ogawa.a7517.widget.PointView;
import com.ogawa.a7517.widget.TimeDialog;

import java.util.ArrayList;
import java.util.List;

public class SeniorMassageFragment extends BaseFragment implements View.OnClickListener {
    private ViewPager2 vpSenior;
    private FragmentAdapter adapter;
    private int currentPage;
    private TimeDialog timeDialog;
    private TextView tvTime;
    private ImageView iv3DLev, ivGasLev, ivKneadLev, ivKnockLev;
    private ImageView ivHandRollLeft, ivHandRollRight, ivFootRollLeft, ivFootRollRight;
    private ImageView ivUp, ivDown;
    private TextView tv3DLev, tvGasLev, tvKneadLev, tvKnockLev;
    private ArcSeekBar asb3D, asbGas, asbKnead, asbKnock;
    private ImageView ivBackHeat, ivKneeHeat;
    private PointView pointView;
    private GasView gasView;
    private AnimatorSet footAnimatorSet;
    private AnimatorSet handAnimatorSet;
    private int[] imgStandardRes = {R.drawable.img_scale_standard_0, R.drawable.img_scale_standard_1
            , R.drawable.img_scale_standard_2, R.drawable.img_scale_standard_3, R.drawable.img_scale_standard_4};
    private int[] imgBigRes = {R.drawable.img_scale_big_1, R.drawable.img_scale_big_1, R.drawable.img_scale_big_2
            , R.drawable.img_scale_big_3, R.drawable.img_scale_big_4, R.drawable.img_scale_big_5};
    private List<Fragment> fragments;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_senior_massage;
    }

    @Override
    public void initView(View view) {
        TextView tvTitle = view.findViewById(R.id.tv_title);
        tvTitle.setText(R.string.senior_massage);
        view.findViewById(R.id.iv_bg_time).setOnClickListener(this);
        tvTime = view.findViewById(R.id.tv_time);
        vpSenior = view.findViewById(R.id.vp_senior);
        fragments = new ArrayList<>();
        fragments.add(new MassageSkillFragment());
        fragments.add(new GasSkillFragment());
        fragments.add(new SeniorAdjustFragment());
        fragments.add(new RollHeatFragment());
        adapter = new FragmentAdapter(this, fragments);
        vpSenior.setAdapter(adapter);
        RecyclerView rlvContent = (RecyclerView) vpSenior.getChildAt(0);
        int padding = ViewUtil.dip2px(25, getContext());
        rlvContent.setPadding(padding, 0, padding, 0);
        rlvContent.setClipToPadding(false);
        vpSenior.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                currentPage = position;
            }
        });
        ivHandRollLeft = view.findViewById(R.id.iv_hand_roll_left);
        ivHandRollRight = view.findViewById(R.id.iv_hand_roll_right);
        ivFootRollLeft = view.findViewById(R.id.iv_foot_roll_left);
        ivFootRollRight = view.findViewById(R.id.iv_foot_roll_right);
        iv3DLev = view.findViewById(R.id.iv_3d);
        ivGasLev = view.findViewById(R.id.iv_gas_lev);
        ivKneadLev = view.findViewById(R.id.iv_knead_speed);
        ivKnockLev = view.findViewById(R.id.iv_knock_strength);
        tv3DLev = view.findViewById(R.id.tv_3d_lev_num);
        tvGasLev = view.findViewById(R.id.tv_gas_lev_num);
        tvKneadLev = view.findViewById(R.id.tv_knead_speed_num);
        tvKnockLev = view.findViewById(R.id.tv_knock_strength_num);
        asb3D = view.findViewById(R.id.asb_3d_lev);
        asbGas = view.findViewById(R.id.asb_gas_lev);
        asbKnead = view.findViewById(R.id.asb_knead_lev);
        asbKnock = view.findViewById(R.id.asb_knock_strength);
        ivUp = view.findViewById(R.id.iv_up);
        ivUp.setOnClickListener(this);
        ivDown = view.findViewById(R.id.iv_down);
        ivDown.setOnClickListener(this);
        ivBackHeat = view.findViewById(R.id.iv_back_heat);
        ivKneeHeat = view.findViewById(R.id.iv_knee_heat);
        pointView = view.findViewById(R.id.point_view);
        gasView = view.findViewById(R.id.gas_view);
        initHandAnimator();//初始化手部滚轮动画
        initFootAnimator();//初始化脚底滚轮动画
        initAsbProgressChangeListener();
        deviceStateChange();
    }

    private void initAsbProgressChangeListener() {
        asbGas.setOnProgressChangeListener(new ArcSeekBar.OnProgressChangeListener() {
            @Override
            public void onProgressChanged(ArcSeekBar seekBar, int progress, boolean isUser) {

            }

            @Override
            public void onStartTrackingTouch(ArcSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(ArcSeekBar seekBar) {
                activity7517.publish(Constant.AIR_INTENSITY, "1", String.valueOf(seekBar.getProgress()));
            }
        });
        asb3D.setOnProgressChangeListener(new ArcSeekBar.OnProgressChangeListener() {
            @Override
            public void onProgressChanged(ArcSeekBar seekBar, int progress, boolean isUser) {

            }

            @Override
            public void onStartTrackingTouch(ArcSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(ArcSeekBar seekBar) {
                activity7517.publish(Constant._3D_STRENGTH_LEVEL, "1", String.valueOf(seekBar.getProgress()));
            }
        });
        asbKnead.setOnProgressChangeListener(new ArcSeekBar.OnProgressChangeListener() {
            @Override
            public void onProgressChanged(ArcSeekBar seekBar, int progress, boolean isUser) {

            }

            @Override
            public void onStartTrackingTouch(ArcSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(ArcSeekBar seekBar) {
                activity7517.publish(Constant.XYZ_SPEED_LEVEL, "1", String.valueOf(seekBar.getProgress()));
            }
        });
        asbKnock.setOnProgressChangeListener(new ArcSeekBar.OnProgressChangeListener() {
            @Override
            public void onProgressChanged(ArcSeekBar seekBar, int progress, boolean isUser) {

            }

            @Override
            public void onStartTrackingTouch(ArcSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(ArcSeekBar seekBar) {
                activity7517.publish(Constant.KNOCK_SPEED_LEVEL_ICON, "6", String.valueOf(seekBar.getProgress()));
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        setTitle("");
        vpSenior.setAdapter(adapter);
        vpSenior.setCurrentItem(currentPage, false);
    }

    @Override
    public void onStop() {
        super.onStop();
        vpSenior.setAdapter(null);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_bg_time) {
            showTimeDialog();
        } else if (id == R.id.iv_up) {
            activity7517.publish(Constant.Y_STATUS, "1");
        } else if (id == R.id.iv_down) {
            activity7517.publish(Constant.Y_STATUS, "2");
        }
    }

    private void showTimeDialog() {
        if (timeDialog == null) {
            timeDialog = new TimeDialog(activity7517);
        }
        timeDialog.show();
    }

    @Override
    public void deviceStateChange() {
        super.deviceStateChange();
        DeviceStateBean stateBean = MassageArmchair.getInstance().getDeviceStatusBean();
        if (stateBean != null) {
            if (stateBean.getFunctions().getAirIntensity() != null) {//充气强度
                int gasLev = Integer.parseInt(stateBean.getFunctions().getAirIntensity().getModelValue());
                tvGasLev.setText(String.valueOf(gasLev));
                asbGas.setProgress(gasLev);
                ivGasLev.setImageResource(imgStandardRes[Math.min(gasLev, 4)]);
            }
            if (stateBean.getFunctions().get_$3DStrengthLevel() != null) {//3D强度
                int _3DLev = Integer.parseInt(stateBean.getFunctions().get_$3DStrengthLevel().getModelValue());
                tv3DLev.setText(String.valueOf(_3DLev));
                asb3D.setProgress(_3DLev);
                iv3DLev.setImageResource(imgBigRes[Math.min(_3DLev, 5)]);
            }
            if (stateBean.getFunctions().getXyzSpeedLevel() != null) {//揉捏速度
                int kneadLev = Integer.parseInt(stateBean.getFunctions().getXyzSpeedLevel().getModelValue());
                tvKneadLev.setText(String.valueOf(kneadLev));
                asbKnead.setProgress(kneadLev);
                ivKneadLev.setImageResource(imgStandardRes[Math.min(kneadLev, 4)]);
            }
            if (stateBean.getFunctions().getKnockSpeedLevelIcon() != null) {//敲击速度
                int knockLev = Integer.parseInt(stateBean.getFunctions().getKnockSpeedLevelIcon().getModelValue());
                tvKnockLev.setText(String.valueOf(knockLev));
                asbKnock.setProgress(knockLev);
                ivKnockLev.setImageResource(imgStandardRes[Math.min(knockLev, 4)]);
            }
            DeviceStateBean.FunctionsBean.TimeRemainingBean timeRemaining = stateBean.getFunctions().getTimeRemaining();
            if (timeRemaining != null) {
                tvTime.setText(timeRemaining.getModelValue());
            }
            if (stateBean.getFunctions().getYStatus() != null) {
                int yStatus = Integer.parseInt(stateBean.getFunctions().getYStatus().getModelValue());
                ivUp.setSelected(yStatus == 1);
                ivDown.setSelected(yStatus == 2);
            }
            DeviceStateBean.FunctionsBean.YPosBean yPos = stateBean.getFunctions().getYPos();
            if (yPos != null) {
                int y = Integer.parseInt(yPos.getModelValue());
                pointView.setPointVis(y);
            }
            gasView.deviceStateChange();
            if (stateBean.getFunctions().getHeatBack() != null) {//背部加热
                int heatBack = Integer.parseInt(stateBean.getFunctions().getHeatBack().getModelValue());
                ivBackHeat.setVisibility(heatBack == 1 ? View.VISIBLE : View.GONE);
            }
            if (stateBean.getFunctions().getKneeHeat() != null) {//膝部加热
                int kneeHeat = Integer.parseInt(stateBean.getFunctions().getKneeHeat().getModelValue());
                ivKneeHeat.setVisibility(kneeHeat == 1 ? View.VISIBLE : View.GONE);
            }
            if (stateBean.getFunctions().getHandScroll() != null) {//手部滚轮
                int handScroll = Integer.parseInt(stateBean.getFunctions().getHandScroll().getModelValue());
                ivHandRollLeft.setSelected(handScroll == 1);
                ivHandRollRight.setSelected(handScroll == 1);
                if (handScroll == 1) {
                    if (!handAnimatorSet.isRunning()) {
                        handAnimatorSet.start();
                    }
                } else {
                    handAnimatorSet.cancel();
                }
            }
            if (stateBean.getFunctions().getFootWheel() != null) {//脚底滚轮
                int footWheel = Integer.parseInt(stateBean.getFunctions().getFootWheel().getModelValue());
                ivFootRollLeft.setSelected(footWheel == 1);
                ivFootRollRight.setSelected(footWheel == 1);
                if (footWheel == 1) {
                    if (!footAnimatorSet.isRunning()) {
                        footAnimatorSet.start();
                    }
                } else {
                    footAnimatorSet.cancel();
                }
            }
        } else {
            stateReset();
        }
        int runningStatus = MassageArmchair.getInstance().getRunningStatus();
        if (runningStatus == 0) {
            stateReset();
        }
        for (Fragment fragment : fragments) {
            if (fragment instanceof BaseFragment) {
                ((BaseFragment) fragment).deviceStateChange();
            }
        }
    }

    private void stateReset() {
        asb3D.setProgress(0);
        tv3DLev.setText("0");
        asbGas.setProgress(0);
        tvGasLev.setText("0");
        asbKnead.setProgress(0);
        tvKneadLev.setText("0");
        asbKnock.setProgress(0);
        tvKnockLev.setText("0");
        gasView.stateReset();
        pointView.setPointVis(-1);
        ivHandRollLeft.setSelected(false);
        ivHandRollRight.setSelected(false);
        ivFootRollLeft.setSelected(false);
        ivFootRollRight.setSelected(false);
        ivBackHeat.setVisibility(View.GONE);
        ivKneeHeat.setVisibility(View.GONE);
        footAnimatorSet.cancel();
        handAnimatorSet.cancel();
    }

    private void initFootAnimator() {
        footAnimatorSet = new AnimatorSet();
        ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(ivFootRollLeft, "rotation", 0, 720);
        ObjectAnimator rotate2Anim = ObjectAnimator.ofFloat(ivFootRollRight, "rotation", 0, 720);
        rotateAnim.setRepeatCount(ValueAnimator.INFINITE);
        rotate2Anim.setRepeatCount(ValueAnimator.INFINITE);
        rotateAnim.setRepeatMode(ValueAnimator.RESTART);
        rotate2Anim.setRepeatMode(ValueAnimator.RESTART);
        footAnimatorSet.setDuration(1000);
        footAnimatorSet.play(rotateAnim).with(rotate2Anim);
    }

    private void initHandAnimator() {
        handAnimatorSet = new AnimatorSet();
        ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(ivHandRollLeft, "rotation", 0, 720);
        ObjectAnimator rotate2Anim = ObjectAnimator.ofFloat(ivHandRollRight, "rotation", 0, 720);
        rotateAnim.setRepeatCount(ValueAnimator.INFINITE);
        rotate2Anim.setRepeatCount(ValueAnimator.INFINITE);
        rotateAnim.setRepeatMode(ValueAnimator.RESTART);
        rotate2Anim.setRepeatMode(ValueAnimator.RESTART);
        handAnimatorSet.setDuration(1000);
        handAnimatorSet.play(rotateAnim).with(rotate2Anim);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (footAnimatorSet != null) {
            footAnimatorSet.cancel();
        }
        if (handAnimatorSet != null) {
            handAnimatorSet.cancel();
        }
    }
}
